package org.jeecg.modules.veterans.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.veterans.entity.SysActivities;
import org.jeecg.modules.veterans.entity.SysProblem;
import org.jeecg.modules.veterans.service.ISysProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * 名称
 *
 * @author: 仝明明
 * @Date:Create in 10:29 AM 2023/10/9
 */

@Api(tags="问题反馈表")
@RestController
@RequestMapping("/veterans/sysProblem")
@Slf4j
public class SysProblemController {
    @Autowired
    ISysProblemService problemService;


    /**
     * 分页列表查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value="问题反馈-分页列表查询", notes="问题反馈-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SysProblem>> queryPageList(SysProblem sysProblem,
                                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                   HttpServletRequest req) {
        QueryWrapper<SysProblem> queryWrapper = QueryGenerator.initQueryWrapper(sysProblem, req.getParameterMap());
        Page<SysProblem> page = new Page<SysProblem>(pageNo, pageSize);
        IPage<SysProblem> pageList = problemService.page(page,queryWrapper);
        return Result.OK(pageList);
    }


    /**
     *   添加
     *
     * @return
     */
    @AutoLog(value = "问题反馈-添加")
    @ApiOperation(value="问题反馈-添加", notes="问题反馈-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SysProblem problem) {
        problem.setStatus(0);
        problemService.save(problem);
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     *
     * @param problem
     * @return
     */
    @AutoLog(value = "问题反馈-处理结果")
    @ApiOperation(value="问题反馈-处理结果", notes="问题反馈-处理结果")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<String> edit(@RequestBody SysProblem problem) {
        if(StringUtils.isEmpty(problem.getResult())){
            return Result.error("处理结果不能为空!");
        }
        problem.setStatus(1);
        problemService.updateById(problem);
        return Result.OK("处理成功!");
    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "问题反馈-通过id删除")
    @ApiOperation(value="问题反馈-通过id删除", notes="问题反馈-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name="id",required=true) String id) {
        problemService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     *  批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "问题反馈-批量删除")
    @ApiOperation(value="问题反馈-批量删除", notes="问题反馈-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.problemService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }


}
